home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C21 / SortedSearchTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.2 KB  |  43 lines

  1. //: C21:SortedSearchTest.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. //{L} ../C20/StreamTokenizer
  7. // Test searching in sorted ranges
  8. #include "../C20/StreamTokenizer.h"
  9. #include "PrintSequence.h"
  10. #include "NString.h"
  11. #include "../require.h"
  12. #include <algorithm>
  13. #include <fstream>
  14. #include <queue>
  15. #include <vector>
  16. using namespace std;
  17.  
  18. int main() {
  19.   ifstream in("SortedSearchTest.cpp");
  20.   assure(in, "SortedSearchTest.cpp");
  21.   StreamTokenizer words(in);
  22.   deque<NString> dstr;
  23.   string word;
  24.   while((word = words.next()).size() != 0)
  25.     dstr.push_back(NString(word));
  26.   vector<NString> v(dstr.begin(), dstr.end());
  27.   sort(v.begin(), v.end());
  28.   print(v, "sorted");
  29.   typedef vector<NString>::iterator sit;
  30.   sit it, it2;
  31.   string f("include");
  32.   cout << "binary search: " 
  33.     << binary_search(v.begin(), v.end(), f) 
  34.     << endl;
  35.   it = lower_bound(v.begin(), v.end(), f);
  36.   it2 = upper_bound(v.begin(), v.end(), f);
  37.   print(it, it2, "found range");
  38.   pair<sit, sit> ip = 
  39.     equal_range(v.begin(), v.end(), f);
  40.   print(ip.first, ip.second, 
  41.     "equal_range");
  42. } ///:~
  43.